package string;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: ycy
 * @CreateDate: 2019/3/27 11:14
 */
public class RegexDemo {
  public static void main(String[] args) {
    RegexDemo regexDemo = new RegexDemo();
    regexDemo.test();
  }

  public void test() {
    String str = "142453@@@曲阜三孔联票成人票(实体票入园)\n" +
            "5925@@@一江四湖8:00-12:00豪华空调船票儿童票(8:00~12:00)";
    //第一步过滤
    Pattern compileFilter =
            Pattern.compile(
                    "\\d{1,2}[：:]{1}\\d{1,2}[-~]+\\d{1,2}[：:]{1}\\d{1,2}");
    str = compileFilter.matcher(str).replaceAll("");
    System.out.println(str);
  }

  public void test1() {
    String str = "\"C区儿童票(17:00场,不可单独预订)(12周岁及以下)（哈哈）\"";
    System.out.println(str);
    String regex = "(\\(.*?\\))|(（.*?）)";
    //采用Matcher 中的Matches方法
    System.out.println(Pattern.matches(regex, str));
    //Matcher 有matches lookingAt find 三个用法
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(str);
//    //整体匹配
//    System.out.println(pattern.matcher(str).matches());

//    //lookingAt()对前面的字符串进行匹配,只有匹配到的字符串在最前面才返回true
//    System.out.println(pattern.matcher(str).lookingAt());

    //部分匹配
//    System.out.println(pattern.matcher(str).start(0));
    List<String> content = Lists.newLinkedList();
    while (matcher.find()) {
      System.out.println(matcher.group());
    }
  }

  public void test2() {
    String str = new String("\"4995@@@曲阜三孔联票成人票(实体票入园)\"");
    System.out.println(str);
    //第一步过滤
    Pattern compileFilter = Pattern.compile("(\\d{2}:\\d{2})|儿童票|成人票|");
    str = compileFilter.matcher(str).replaceAll("");


    Pattern compile = Pattern.compile("(\\(.*?\\))|(（.*?）)");
    Matcher matcher = compile.matcher(str);
    //舍去最后一个括号里面的内容
    List<String> contents = Lists.newLinkedList();
    int parenthesesCount = matcher.groupCount();
    int lastLeftIndex = 0;
    if (parenthesesCount > 1) {
      while (matcher.find()) {
        contents.add(matcher.group());
        lastLeftIndex = matcher.start();
      }
    }
    if (lastLeftIndex != 0) {
      str = str.substring(0, lastLeftIndex);
    }

    //
    str = str.replaceAll("\\(\\)", "");
    System.out.println("结果== " + str);
  }

  class ParenthesesContent {
    String name;
    int start;
    int end;

    public ParenthesesContent(String group, int start, int end) {
      this.name = group;
      this.start = start;
      this.end = end;
    }
  }

  public void test3() {

  }

}
